package org.ala.shopping_test.user_center.service;

import org.ala.shopping_test.ShardInfos;
import org.ala.shopping_test.user_center.repository.user.dao.UserBuyerMapper;
import org.ala.shopping_test.user_center.repository.user.dao.UserSellerMapper;
import org.ala.shopping_test.user_center.repository.user.entity.UserBuyerDO;
import org.ala.shopping_test.user_center.repository.user.entity.UserBuyerFields;
import org.ala.shopping_test.user_center.repository.user.entity.UserSellerDO;
import org.ala.shopping_test.user_center.repository.user.entity.UserSellerFields;
import org.ala.shopping_test.user_center.repository.user.query.UserBuyerQuery;
import org.ala.shopping_test.user_center.repository.user.query.UserSellerQuery;
import org.ala.tiktools.logs.LoggerFactory;
import org.ala.tiktools.sharddb.db.ThreadLocalLookupKey;
import org.ala.tiktools.sharddb.tools.SnowIdGenerator;
import org.ala.tiktools.tools.TimestampTools;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ala
 * @date 2025-03-17 09:38
 */
@Component
public class UserService {


    static Logger log = LoggerFactory.getLogger("shopping", "user");


    @Autowired
    protected UserBuyerMapper userBuyerMapper;
    @Autowired
    protected UserSellerMapper userSellerMapper;

    protected SnowIdGenerator idGenerator = SnowIdGenerator.build(0);


    public void createData(int size) {
        createBuyerData(size);
        createSellerData(size);
    }
    public void createSellerData(int size) {
        ThreadLocalLookupKey.setLookup(ShardInfos.DB_USER_CENTER[0]);
        int batch = 1000;
        try {
            List<UserSellerDO> sellers = new ArrayList<>();
            for (int i = 0 ; i < size ; i++) {
                SnowIdGenerator.Id id = idGenerator.id();
                sellers.add(UserSellerDO.build()
                        .id(id.getId())
                        .sellerName(String.format("买家_%d", i))
                        .sellerLevel((i % 5) + 1)
                        .createTime(id.getTimestamp())
                        .modifyTime(id.getTimestamp()));
                if ((i+1) % batch == 0) {
                    userSellerMapper.multipleInsert(sellers);
                    log.info(String.format("[UserService createSellerData] 构造测试数据 size:%d", sellers.size()));
                    sellers.clear();
                }
            }
            if (!sellers.isEmpty()) {
                userSellerMapper.multipleInsert(sellers);
                log.info(String.format("[UserService createSellerData] 构造测试数据 size:%d", sellers.size()));
                sellers.clear();
            }
        } finally {
            ThreadLocalLookupKey.clear();
        }
    }
    public void createBuyerData(int size) {
        ThreadLocalLookupKey.setLookup(ShardInfos.DB_USER_CENTER[0]);
        int batch = 1000;
        try {
            List<UserBuyerDO> buyers = new ArrayList<>();
            for (int i = 0 ; i < size ; i++) {
                SnowIdGenerator.Id id = idGenerator.id();
                buyers.add(UserBuyerDO.build()
                                .id(id.getId())
                                .buyerName(String.format("买家_%d", i))
                                .buyerLevel((i % 5) + 1)
                                .createTime(id.getTimestamp())
                                .modifyTime(id.getTimestamp()));
                if ((i+1) % batch == 0) {
                    userBuyerMapper.multipleInsert(buyers);
                    log.info(String.format("[UserService createBuyerData] 构造测试数据 size:%d", buyers.size()));
                    buyers.clear();
                }
            }
            if (!buyers.isEmpty()) {
                userBuyerMapper.multipleInsert(buyers);
                log.info(String.format("[UserService createBuyerData] 构造测试数据 size:%d", buyers.size()));
                buyers.clear();
            }
        } finally {
            ThreadLocalLookupKey.clear();
        }
    }


    /**
     *  取id和name
     */
    public List<UserBuyerDO> allBuyer() {
        ThreadLocalLookupKey.setLookup(ShardInfos.DB_USER_CENTER[0]);
        List<UserBuyerDO> list = userBuyerMapper.findByQuery(UserBuyerFields.Id().buyerName().createTime(), UserBuyerQuery.build());
        ThreadLocalLookupKey.clear();
        return list;
    }
    /**
     *  取id和name
     */
    public List<UserSellerDO> allSeller() {
        ThreadLocalLookupKey.setLookup(ShardInfos.DB_USER_CENTER[0]);
        List<UserSellerDO> list = userSellerMapper.findByQuery(UserSellerFields.Id().sellerName().createTime(), UserSellerQuery.build());
        ThreadLocalLookupKey.clear();
        return list;
    }
}
